package com.block.datapush.form.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.service.LogFactory;
import com.block.datapush.model.dto.FormItemDTO;
import com.block.datapush.form.service.IFormInfoItemService;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoItem;
import com.block.db.entity.MemberExtInfo;
import com.block.db.mapper.FormInfoItemMapper;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-11-20
 */
@Service
public class FormInfoItemServiceImpl extends ServiceImpl<FormInfoItemMapper, FormInfoItem> implements IFormInfoItemService {

    private static final Logger log = LogFactory.getCommLog();

    @Override
    public FormItemDTO canPushMemberInfo(FormInfo formInfo, MemberExtInfo memberExtInfo){

        FormItemDTO  formItemDTO = new FormItemDTO();
        formItemDTO.setCanPush(false);

        List<FormInfoItem> formInfoItemList = this.baseMapper.getEntityListByFormId(formInfo.getId());

        FormInfoItem resultItem = null;
        boolean canPush = false;
        for (FormInfoItem formInfoItem : formInfoItemList) {

            Integer nowDate = LocalDate.now().getDayOfWeek().getValue();
            String notDateStr = formInfoItem.getNotDate();
            List<Integer > notDateList = JSON.parseArray(notDateStr, Integer.class);
            if (CollUtil.isNotEmpty(notDateList) && notDateList.contains(nowDate)){
                log.info("【表单推送】今日在不推送日期内【nowDate】{}【notDateStr】{}", nowDate, notDateStr);
                continue;
            }

            boolean result = this.pushFormItem(formInfoItem, memberExtInfo);
            if (result){
                resultItem = formInfoItem;
                canPush = result;
                break;
            }
        }

        if (!canPush){
            return formItemDTO;
        }

        formItemDTO.setCanPush(true);
        formItemDTO.setItemId(resultItem.getId());
        formItemDTO.setPriceAmount(resultItem.getPriceAmount());

        return formItemDTO;
    }

    /**
     *
     * @param formInfoItem
     * @param memberExtInfo
     * @return
     */
    public boolean pushFormItem(FormInfoItem formInfoItem, MemberExtInfo memberExtInfo){

        // 判断城市
        boolean canPush = true;
        if (StrUtil.isNotEmpty(formInfoItem.getCityArr())){
            List<String> cityList = JSON.parseArray(formInfoItem.getCityArr(), String.class);
            if (!((cityList.contains("*")) ||
                    (cityList.contains(memberExtInfo.getProvince()) ||
                            cityList.contains(memberExtInfo.getCity())))){
                log.info("【formItem推送判断】城市判断，不可推送【itemCityList】{}【city】{}【province】{}", formInfoItem.getCityArr(), memberExtInfo.getCity(), memberExtInfo.getProvince());
                canPush = false;
            }
        }
        if (StrUtil.isNotEmpty(formInfoItem.getLoanAmount())){
            List<String> loanAmountList = JSON.parseArray(formInfoItem.getLoanAmount(), String.class);
            if (!loanAmountList.contains(memberExtInfo.getLoanAmount())){
                log.info("【formItem推送判断】贷款额度判断，不可推送【itemLoanAmount】{}【loanAmount】{}", formInfoItem.getLoanAmount(), memberExtInfo.getLoanAmount());
                canPush = false;
            }
        }

        if (StrUtil.isNotEmpty(formInfoItem.getHouse())){
            List<String> houseList = JSON.parseArray(formInfoItem.getHouse(), String.class);
            if (!houseList.contains(memberExtInfo.getHouse())){
                log.info("【formItem推送判断】房产判断，不可推送【itemHouse】{}【house】{}", formInfoItem.getHouse(), memberExtInfo.getHouse());
                canPush = false;
            }
        }

        if (StrUtil.isNotEmpty(formInfoItem.getGjj())){
            List<String> gjjList = JSON.parseArray(formInfoItem.getGjj(), String.class);
            if (!gjjList.contains(memberExtInfo.getGjj())){
                log.info("【formItem推送判断】公积金判断，不可推送【itemGjj】{}【gjj】{}", formInfoItem.getGjj(), memberExtInfo.getGjj());
                canPush = false;
            }
        }
        if (StrUtil.isNotEmpty(formInfoItem.getZhima())){
            List<String> zhimaList = JSON.parseArray(formInfoItem.getZhima(), String.class);
            if (!zhimaList.contains(memberExtInfo.getZhimaScore())){
                log.info("【formItem推送判断】芝麻判断，不可推送【itemZhima】{}【zhima】{}", formInfoItem.getZhima(), memberExtInfo.getZhimaScore());
                canPush = false;
            }
        }

        if (StrUtil.isNotEmpty(formInfoItem.getCar())){
            List<String> carList = JSON.parseArray(formInfoItem.getCar(), String.class);
            if (!carList.contains(memberExtInfo.getCar())){
                log.info("【formItem推送判断】车判断，不可推送【itemCar】{}【car】{}", formInfoItem.getCar(), memberExtInfo.getCar());
                canPush = false;
            }
        }

        if (StrUtil.isNotEmpty(formInfoItem.getYuqi())){
            List<String> yuqiList = JSON.parseArray(formInfoItem.getYuqi(), String.class);
            if (!yuqiList.contains(memberExtInfo.getYuqi())){
                log.info("【formItem推送判断】逾期判断，不可推送【itemYuqi】{}【yuqi】{}", formInfoItem.getYuqi(), memberExtInfo.getYuqi());
                canPush = false;
            }
        }

        if (StrUtil.isNotEmpty(formInfoItem.getAgeInfo())){
            try {
                String ageInfo = formInfoItem.getAgeInfo();
                JSONObject jsonObject = JSON.parseObject(ageInfo);
                Integer minAge = jsonObject.getInteger("minAge");
                Integer maxAge = jsonObject.getInteger("maxAge");

                Integer age = IdcardUtil.getAgeByIdCard(memberExtInfo.getIdCard());
                if (age < minAge || age > maxAge){
                    log.info("【formItem推送判断】年龄判断，不可推送【itemAge】{}【age】{}", formInfoItem.getAgeInfo(), age);
                    canPush = false;
                }
            }catch (Exception exception){
                exception.printStackTrace();
            }
        }

        if (StrUtil.isNotEmpty(formInfoItem.getOutCityArray())){
            List<String> outCityArrayList = JSON.parseArray(formInfoItem.getOutCityArray(), String.class);
            if (outCityArrayList.contains(memberExtInfo.getCityCode()) ||
                    outCityArrayList.contains(memberExtInfo.getProvinceCode())){
                log.info("【formItem推送判断不推送城市判断，不可推送【itemOutCity】{}【cityCode】{}【provinceCode】{}",
                        formInfoItem.getOutCityArray(), memberExtInfo.getCityCode(), memberExtInfo.getProvinceCode());
                canPush = false;
            }
        }

        if (StrUtil.isNotEmpty(formInfoItem.getOutChannelCode())){
            List<String> outChannelCodeList = JSON.parseArray(formInfoItem.getOutChannelCode(), String.class);
            if (outChannelCodeList.contains(memberExtInfo.getChannelCode())){
                log.info("【formItem推送判断不推送渠道判断，不可推送【itemOutChannelCode】{}【channelCode】{}",
                        formInfoItem.getOutChannelCode(), memberExtInfo.getChannelCode());
                canPush = false;
            }
        }

        if (canPush){
            log.info("【formItem推送判断】可推送【item】{}", JSON.toJSONString(formInfoItem));
        }

        return canPush;
    }

}
